package ch.quantasy.tinkerforge.tinker;

import com.tinkerforge.BrickDC;
import com.tinkerforge.BrickIMU;
import com.tinkerforge.BrickMaster;
import com.tinkerforge.BrickServo;
import com.tinkerforge.BrickStepper;
import com.tinkerforge.BrickletAmbientLight;
import com.tinkerforge.BrickletAnalogIn;
import com.tinkerforge.BrickletAnalogOut;
import com.tinkerforge.BrickletBarometer;
import com.tinkerforge.BrickletCurrent12;
import com.tinkerforge.BrickletCurrent25;
import com.tinkerforge.BrickletDistanceIR;
import com.tinkerforge.BrickletDistanceUS;
import com.tinkerforge.BrickletDualButton;
import com.tinkerforge.BrickletDualRelay;
import com.tinkerforge.BrickletGPS;
import com.tinkerforge.BrickletHallEffect;
import com.tinkerforge.BrickletHumidity;
import com.tinkerforge.BrickletIO16;
import com.tinkerforge.BrickletIO4;
import com.tinkerforge.BrickletIndustrialDigitalIn4;
import com.tinkerforge.BrickletIndustrialDigitalOut4;
import com.tinkerforge.BrickletIndustrialDual020mA;
import com.tinkerforge.BrickletIndustrialQuadRelay;
import com.tinkerforge.BrickletJoystick;
import com.tinkerforge.BrickletLCD16x2;
import com.tinkerforge.BrickletLCD20x4;
import com.tinkerforge.BrickletLEDStrip;
import com.tinkerforge.BrickletLine;
import com.tinkerforge.BrickletLinearPoti;
import com.tinkerforge.BrickletMoisture;
import com.tinkerforge.BrickletMotionDetector;
import com.tinkerforge.BrickletMultiTouch;
import com.tinkerforge.BrickletPTC;
import com.tinkerforge.BrickletPiezoBuzzer;
import com.tinkerforge.BrickletPiezoSpeaker;
import com.tinkerforge.BrickletRemoteSwitch;
import com.tinkerforge.BrickletRotaryEncoder;
import com.tinkerforge.BrickletRotaryPoti;
import com.tinkerforge.BrickletSegmentDisplay4x7;
import com.tinkerforge.BrickletSoundIntensity;
import com.tinkerforge.BrickletTemperature;
import com.tinkerforge.BrickletTemperatureIR;
import com.tinkerforge.BrickletTilt;
import com.tinkerforge.BrickletVoltage;
import com.tinkerforge.BrickletVoltageCurrent;
import com.tinkerforge.Device;

/**
 * This enum is used as a convenience to map the Tinker-Bricks and
 * Tinker-Bricklets It tries to hide the 'strange' stuff from the Java-Developer
 * ;-)
 * 
 * @author reto
 * 
 */
public enum TinkerforgeDevice {
	// DC (BrickDC.class, BrickDC.DEVICE_IDENTIFIER) , IMU (BrickIMU.class,
	// BrickIMU.DEVICE_IDENTIFIER) , Master (
	// BrickMaster.class, BrickMaster.DEVICE_IDENTIFIER) , Servo
	// (BrickServo.class, BrickServo.DEVICE_IDENTIFIER) , Stepper (
	// BrickStepper.class, BrickStepper.DEVICE_IDENTIFIER) ,
	//
	// AmbientLight (BrickletAmbientLight.class,
	// BrickletAmbientLight.DEVICE_IDENTIFIER) , AnalogIn (BrickletAnalogIn.class,
	// BrickletAnalogIn.DEVICE_IDENTIFIER) , AnalogOut (BrickletAnalogOut.class,
	// BrickletAnalogOut.DEVICE_IDENTIFIER) , Barometer (
	// BrickletBarometer.class, BrickletBarometer.DEVICE_IDENTIFIER) , Current12
	// (BrickletCurrent12.class,
	// BrickletCurrent12.DEVICE_IDENTIFIER) , Current25 (BrickletCurrent25.class,
	// BrickletCurrent25.DEVICE_IDENTIFIER) , DistanceIR (
	// BrickletDistanceIR.class, BrickletDistanceIR.DEVICE_IDENTIFIER) ,
	// DistanceUS (BrickletDistanceUS.class,
	// BrickletDistanceUS.DEVICE_IDENTIFIER) , DualButton
	// (BrickletDualButton.class,
	// BrickletDualButton.DEVICE_IDENTIFIER) , DualRelay (BrickletDualRelay.class,
	// BrickletDualRelay.DEVICE_IDENTIFIER) , GPS (
	// BrickletGPS.class, BrickletGPS.DEVICE_IDENTIFIER) , HallEffect
	// (BrickletHallEffect.class,
	// BrickletHallEffect.DEVICE_IDENTIFIER) , Humidity (BrickletHumidity.class,
	// BrickletHumidity.DEVICE_IDENTIFIER) , IndustrialDigitalIn4 (
	// BrickletIndustrialDigitalIn4.class,
	// BrickletIndustrialDigitalIn4.DEVICE_IDENTIFIER) , IndustrialDigitalOut4 (
	// BrickletIndustrialDigitalOut4.class,
	// BrickletIndustrialDigitalOut4.DEVICE_IDENTIFIER) , IndustrialDual020mA (
	// BrickletIndustrialDual020mA.class,
	// BrickletIndustrialDual020mA.DEVICE_IDENTIFIER) , IndustrialQuadRelay (
	// BrickletIndustrialQuadRelay.class,
	// BrickletIndustrialQuadRelay.DEVICE_IDENTIFIER) , IO16 (BrickletIO16.class,
	// BrickletIO16.DEVICE_IDENTIFIER) , IO4 (BrickletIO4.class,
	// BrickletIO4.DEVICE_IDENTIFIER) , Joystick (
	// BrickletJoystick.class, BrickletJoystick.DEVICE_IDENTIFIER) , LCD16x2
	// (BrickletLCD16x2.class,
	// BrickletLCD16x2.DEVICE_IDENTIFIER) , LCD20x4 (BrickletLCD20x4.class,
	// BrickletLCD20x4.DEVICE_IDENTIFIER) , LEDStrip (
	// BrickletLEDStrip.class, BrickletLEDStrip.DEVICE_IDENTIFIER) , Line
	// (BrickletLine.class,
	// BrickletLine.DEVICE_IDENTIFIER) , LinearPoti (BrickletLinearPoti.class,
	// BrickletLinearPoti.DEVICE_IDENTIFIER) , Moisture (
	// BrickletMoisture.class, BrickletMoisture.DEVICE_IDENTIFIER) ,
	// MotionDetector (BrickletMotionDetector.class,
	// BrickletMotionDetector.DEVICE_IDENTIFIER) , MultiTouch
	// (BrickletMultiTouch.class,
	// BrickletMultiTouch.DEVICE_IDENTIFIER) , PiezoBuzzer
	// (BrickletPiezoBuzzer.class,
	// BrickletPiezoBuzzer.DEVICE_IDENTIFIER) , PiezoSpeaker
	// (BrickletPiezoSpeaker.class,
	// BrickletPiezoSpeaker.DEVICE_IDENTIFIER) , PTC (BrickletPTC.class,
	// BrickletPTC.DEVICE_IDENTIFIER) , RemoteSwitch (
	// BrickletRemoteSwitch.class, BrickletRemoteSwitch.DEVICE_IDENTIFIER) ,
	// RotaryEncoder (BrickletRotaryEncoder.class,
	// BrickletRotaryEncoder.DEVICE_IDENTIFIER) , RotaryPoti
	// (BrickletRotaryPoti.class,
	// BrickletRotaryPoti.DEVICE_IDENTIFIER) , SegmentDisplay4x7
	// (BrickletSegmentDisplay4x7.class,
	// BrickletSegmentDisplay4x7.DEVICE_IDENTIFIER) , SoundIntensity
	// (BrickletSoundIntensity.class,
	// BrickletSoundIntensity.DEVICE_IDENTIFIER) , Temperature
	// (BrickletTemperature.class,
	// BrickletTemperature.DEVICE_IDENTIFIER) , TemperatureIR
	// (BrickletTemperatureIR.class,
	// BrickletTemperatureIR.DEVICE_IDENTIFIER) , Tilt (BrickletTilt.class,
	// BrickletTilt.DEVICE_IDENTIFIER) , Voltage (
	// BrickletVoltage.class, BrickletVoltage.DEVICE_IDENTIFIER) , VoltageCurrent
	// (BrickletVoltageCurrent.class,
	// BrickletVoltageCurrent.DEVICE_IDENTIFIER);
	DC (BrickDC.class) , IMU (BrickIMU.class) , Master (BrickMaster.class) , Servo (BrickServo.class) , Stepper (
			BrickStepper.class) ,

	AmbientLight (BrickletAmbientLight.class) , AnalogIn (BrickletAnalogIn.class) , AnalogOut (BrickletAnalogOut.class) , Barometer (
			BrickletBarometer.class) , Current12 (BrickletCurrent12.class) , Current25 (BrickletCurrent25.class) , DistanceIR (
			BrickletDistanceIR.class) , DistanceUS (BrickletDistanceUS.class) , DualButton (BrickletDualButton.class) , DualRelay (
			BrickletDualRelay.class) , GPS (BrickletGPS.class) , HallEffect (BrickletHallEffect.class) , Humidity (
			BrickletHumidity.class) , IndustrialDigitalIn4 (BrickletIndustrialDigitalIn4.class) , IndustrialDigitalOut4 (
			BrickletIndustrialDigitalOut4.class) , IndustrialDual020mA (BrickletIndustrialDual020mA.class) , IndustrialQuadRelay (
			BrickletIndustrialQuadRelay.class) , IO16 (BrickletIO16.class) , IO4 (BrickletIO4.class) , Joystick (
			BrickletJoystick.class) , LCD16x2 (BrickletLCD16x2.class) , LCD20x4 (BrickletLCD20x4.class) , LEDStrip (
			BrickletLEDStrip.class) , Line (BrickletLine.class) , LinearPoti (BrickletLinearPoti.class) , Moisture (
			BrickletMoisture.class) , MotionDetector (BrickletMotionDetector.class) , MultiTouch (BrickletMultiTouch.class) , PiezoBuzzer (
			BrickletPiezoBuzzer.class) , PiezoSpeaker (BrickletPiezoSpeaker.class) , PTC (BrickletPTC.class) , RemoteSwitch (
			BrickletRemoteSwitch.class) , RotaryEncoder (BrickletRotaryEncoder.class) , RotaryPoti (BrickletRotaryPoti.class) , SegmentDisplay4x7 (
			BrickletSegmentDisplay4x7.class) , SoundIntensity (BrickletSoundIntensity.class) , Temperature (
			BrickletTemperature.class) , TemperatureIR (BrickletTemperatureIR.class) , Tilt (BrickletTilt.class) , Voltage (
			BrickletVoltage.class) , VoltageCurrent (BrickletVoltageCurrent.class);

	public final int identifier;
	public final Class<?> device;

	private TinkerforgeDevice(Class<?> device) {
		if (device == null)
			throw new IllegalArgumentException();
		this.device = device;
		int internalIdentifier = -1;
		try {
			internalIdentifier = device.getField("DEVICE_IDENTIFIER").getInt(null);
		} catch (Exception e) {
			//No identifier
		}
		this.identifier = internalIdentifier;
	}

	public static TinkerforgeDevice getDevice(Device device) {
		if (device == null)
			throw new IllegalArgumentException();
		Class deviceClass = device.getClass();
		for (TinkerforgeDevice tinkerforgeDevice : TinkerforgeDevice.values()) {
			if (deviceClass == tinkerforgeDevice.device)
				return tinkerforgeDevice;
		}
		return null;
	}

	public static TinkerforgeDevice getDevice(int deviceIdentifier) {
		for (TinkerforgeDevice tinkerforgeDevice : TinkerforgeDevice.values()) {
			if (deviceIdentifier == tinkerforgeDevice.identifier)
				return tinkerforgeDevice;
		}
		return null;
	}

}
